package com.tevintao.marketingsystem.task;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import java.util.Random;

import static com.tevintao.marketingsystem.task.WxUiViewConfig.getViewByClassName;
import static com.tevintao.marketingsystem.task.WxUiViewConfig.getViewByIdAndContent;


public class AcceptNearbyFriendTaskProcess extends SetTaskExtra {
    private static AcceptNearbyFriendTaskProcess ourInstance = new AcceptNearbyFriendTaskProcess();

    public static AcceptNearbyFriendTaskProcess getInstance() {
        return ourInstance;
    }

    private AcceptNearbyFriendTaskProcess() {
    }

    private Boolean ifPassNoLoaction = false;//是否按下  跳过按钮   使   无法定位提示框跳过
    private Boolean ifSelectSex = false;//是否已经选择了查看  男  女   或者全部
    private int ifThereIsMore = 0;//给自己打招呼的人 列表中  是否有显示更多人的选项   0代表没有，，1代表有
    private Boolean ifCall = false;//是否打过招呼了。是的话，返回附近人列表，没有的话就 接着打招呼
    private int mSelectPosition = -1;
    private Random mRandom = new Random();





    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {

        LogManager.getInstance().logInfo("BrowFriendTaskProcess processView contextName = " + contextName + " v = " + v.toString());
        //WxUiViewConfig.printViewGroup((ViewGroup) v);

        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inDiscover = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_DISCOVER);
            LogManager.getInstance().logInfo("intab = " + inDiscover);
            if (inDiscover) {
                View nearbyView = getViewByIdAndContent((ViewGroup) v,16908310, "附近的人");
                LogManager.getInstance().logInfo("nearbyView = " + nearbyView);
                if (null != nearbyView) {
                    ViewOperation.clickView(nearbyView);
                    ifPassNoLoaction = false;
                }
            }
            //mSelectPosition = -1;
        }else {
            WxUiViewConfig.printViewGroup((ViewGroup) v);


            if ("android.view.ContextThemeWrapper".equals(contextName)) {
                View nolocationView = getViewByIdAndContent((ViewGroup) v,2131626642, "设置");
                View notishiView = v.findViewById(2131626201);
                View paseView = v.findViewById(2131626641);
                if(nolocationView!=null&&notishiView!=null&&paseView!=null) {
                    LogManager.getInstance().logInfo("微信  弹出  无法定位 提示框");
                    ViewOperation.clickView(paseView);
                    ifPassNoLoaction=true;//每次进入附近的人 重新设置为fasle
                    LogManager.getInstance().logInfo("跳过了提示框");
                }else {
                    View checkboxView = v.findViewById(2131626198);
                    if (null != checkboxView && checkboxView instanceof CheckBox) {
                        LogManager.getInstance().logInfo("发现了  提示弹窗页面  提示查看附近的人功能将获取你的位置信息，你的位置信息会被保留一段时间。通过列表右上角的清除功能可随时手动清除位置信息");
                        CheckBox checkbox = (CheckBox) checkboxView;
                        ViewOperation.clickView(checkbox);
                        LogManager.getInstance().logInfo("勾选了  下次不再提示框");
                    }

                    View sureView = getViewByIdAndContent((ViewGroup) v,2131626642, "确定");
                    //View sureView = v.findViewById(2131626642);
                    if (null != sureView && sureView instanceof Button) {
                        Button surebutton = (Button) sureView;
                        ViewOperation.clickView(surebutton);
                        LogManager.getInstance().logInfo("点击确定按钮    下次不会再弹出提示框");
                    }
                }
                View ifclearhistorylist = getViewByIdAndContent((ViewGroup) v, 2131625950, "确定清空历史列表？");
                View cancelclearhistorylist = getViewByIdAndContent((ViewGroup) v, 2131626641, "取消");
                View clearhistorylist = getViewByIdAndContent((ViewGroup) v, 2131626642, "清空");
                if(ifclearhistorylist!=null&&cancelclearhistorylist!=null&&clearhistorylist!=null) {
                    ViewOperation.clickView(clearhistorylist);
                    taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "已经接收了附近人中给我们打招呼的列表的所有人  并清空了列表");
                }
            } else {
                if ("com.tencent.mm.plugin.nearby.ui.NearbyFriendShowSayHiUI".equals(contextName)){
                    View lookNearbyView = getViewByIdAndContent((ViewGroup) v, 2131626837, "查看附近的人");
                    if(lookNearbyView!=null)
                    {
                        ViewOperation.clickView(lookNearbyView);
                    }

                }else
                if ("com.tencent.mm.plugin.nearby.ui.NearbyFriendsUI".equals(contextName)) {
                    // View searchView = getViewByIdAndContent((ViewGroup) v,2131626835, "暂时没有找到附近也使用该功能的人，请稍后再尝试查看。");
                    WxUiViewConfig.printViewGroup((ViewGroup) v);
                    LogManager.getInstance().logInfo("来没来");
                    View selectSex = getViewByClassName((ViewGroup) v, "ActionMenuItemView");
                    if (null != selectSex) {
                        ViewOperation.clickView(selectSex);
                    }

                    View selectView = v.findViewById(2131624112);
                    if (null != selectView) {
                        switch (0) {
                            case 0:
                                selectView = getViewByIdAndContent((ViewGroup) v, 2131624112, "附近打招呼的人");
                                LogManager.getInstance().logInfo("选择了查看附近打招呼的人");
                                break;
                        }
                        ViewOperation.clickView(selectView);
                    }

                } else if("com.tencent.mm.plugin.nearby.ui.NearbySayHiListUI".equals(contextName)){
                    View lookMoreView = v.findViewById(2131627359);
                    if (null != lookMoreView) {
                        ViewOperation.clickView(lookMoreView);
                        LogManager.getInstance().logInfo("点击了查看更多");

                    }
                    LogManager.getInstance().logInfo("当前位于附近打招呼的人 列表");
                    View listView = v.findViewById(2131626199);
                    LogManager.getInstance().logInfo("listView = " + listView);
                    if (null != listView && listView instanceof ListView) {
                        ListView list = (ListView) listView;
                        if(list.getAdapter().getCount()==0)
                            taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "附近打招呼的人列表中没有好友，没有人给我打招呼，结束任务");
                        if (mSelectPosition < 0) {
                            mSelectPosition = 0;
                        }else if (mSelectPosition < list.getAdapter().getCount() + list.getHeaderViewsCount()){
                            //（mSelectPosition的取值范围为0到list.getAdapter().getCount()-2+list.getHeaderViewsCount） 即是从第一个好友到最后一个好友
                            mSelectPosition++;
                        }else {
                            //以下为清空列表操作
                            mSelectPosition=-1;
                            View clearView = getViewByIdAndContent((ViewGroup) v, 2131624138, "清空");
                            if(clearView!=null) {
                                ViewOperation.clickView(clearView);
                                LogManager.getInstance().logInfo("点击了右上角的清空按钮，请确认是否清空附近打招呼的人列表");
                            }
                        }
                        int first = list.getFirstVisiblePosition();
                        int last = list.getLastVisiblePosition();
                        LogManager.getInstance().logInfo("mSelectPosition < 0 mSelectPosition = " + mSelectPosition + " first = " + first + " last =  " + last + " headerCount = " + list.getHeaderViewsCount() + list.getAdapter().getCount() + "  " + list.getCount() + " " + list.getFooterViewsCount());
                        if (mSelectPosition < first || mSelectPosition > last) {
                            ViewOperation.moveListPosition(list, mSelectPosition);
                            LogManager.getInstance().logInfo("不到位");
                            WxUiViewConfig.printViewGroup((ViewGroup) v);
                        } else {
                            //mSelectPosition = -1;
                            LogManager.getInstance().logInfo("移动到位");

                            View currentvisibleView = list.getChildAt(mSelectPosition);
                            WxUiViewConfig.printViewGroup((ViewGroup) v);
                            //JSONObject joTree = HookWindowManagerGlobal.GetViewJsonTree((ViewGroup) firstView, -1);
                            //HookWindowManagerGlobal.printViewTreeJson(joTree, 0);

                            if (null != currentvisibleView) {
                                ViewOperation.clickView(currentvisibleView);
                            }
                        }
                    }
                }
                else if ("com.tencent.mm.plugin.profile.ui.ContactInfoUI".equals(contextName)) {
                    if (ifCall == false) {
                        LogManager.getInstance().logInfo("点击了附近打招呼的人 列表 中的第"+(mSelectPosition+1)+"人  进入了打招呼界面");
                        //首先找到打招呼按钮
                        View buttonViewCall = v.findViewById(2131625116);
                        View buttonViewThrough = v.findViewById(2131625111);
                        if (null != buttonViewCall && buttonViewCall instanceof Button) {
                            ViewOperation.clickView(buttonViewCall);
                        }
                        if (null != buttonViewThrough && buttonViewThrough instanceof Button){
                            ViewOperation.clickView(buttonViewThrough);
                        }
                        ifCall = true;
                    } else {
                        LogManager.getInstance().logInfo("发送了加人请求后    返回了打招呼界面 ( 如果对方没有设置好友验证 也返回到同一个 activty (但是界面内容稍有不同) 并且直接成为了好友)");
                        //找到返回按钮   按下后会返回附近人好友列表
                        View buttonViewBack = v.findViewById(2131624160);
                        if (null != buttonViewBack) {
                            ViewOperation.clickView(buttonViewBack);
                        }
                        ifCall = false;
                        LogManager.getInstance().logInfo("按下了返回按钮");
                    }
                    WxUiViewConfig.printViewGroup((ViewGroup) v);

                }
                else if ("com.tencent.mm.ui.contact.SayHiEditUI".equals(contextName)) {
                    LogManager.getInstance().logInfo("打招呼内容输入界面");
                    WxUiViewConfig.printViewGroup((ViewGroup) v);

                    View editViewShuRu = v.findViewById(2131627356);
                    if (editViewShuRu instanceof EditText) {
                        EditText textView = (EditText) editViewShuRu;
                        if ("".equals(textView.getText().toString())) {
                            //TextView textView = (TextView) buttonViewFaSong;
                            LogManager.getInstance().logInfo("等待自动输入评论内容 ");
                            String[] str = new String[]{
                                    "嗨，你好", "你好啊", "嗨，好久不见了啊", "最近生活不错啊", "毕业了吧", "现在在哪里工作呢", "你怎么也来这边了", "相逢何必曾相识", "亲，忙什么呢", "四海之内皆朋友"
                            };
                            ViewOperation.setText(textView, str[mRandom.nextInt(10)]);
                        } else {
                            View enterView = v.findViewById(2131624139);

                            TextView enterbutton = (TextView) enterView;
                            if ("发送".equals(enterbutton.getText().toString())) {
                                ViewOperation.clickView(enterbutton);
                                LogManager.getInstance().logInfo("自动输入打招呼内容之后，按下了发送按钮");
                            }
                        }
                    }

                }
            }
        }

         if (WxUiViewConfig.VIEW_ADD_AREA.equals(contextName)) {

            View female = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.VIEW_FEMALE, "女");
            if (null != female) {
                if (female instanceof RadioButton) {
                    RadioButton button = (RadioButton) female;
                    if (!button.isChecked()) {
                        ViewOperation.clickView(female);
                    }
                }
            }


            View showCity = v.findViewById(WxUiViewConfig.VIEW_SHOW_CITY);
            if (null != showCity && showCity.getVisibility() == View.VISIBLE && showCity instanceof TextView) {
                TextView selectedCity = (TextView) showCity;
                LogManager.getInstance().logInfo("selectedCity.getText().toString() = " + selectedCity.getText().toString());
                if (!TextUtils.isEmpty(selectedCity.getText().toString().trim())) {
                    LogManager.getInstance().logInfo("定位成功");
//                    WxUiViewConfig.printViewGroup((ViewGroup) v);
                    View noSign = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.VIEW_SELECT_SIGN, "未填写");
                    if (null != noSign) {
                        ViewOperation.clickView(noSign);
                        LogManager.getInstance().logInfo("需要签名");
                        return true;
                    }
                    View nextStep = v.findViewById(WxUiViewConfig.VIEW_SELECT_CITY_NEXT);
                    if (null != nextStep) {
                        ViewOperation.clickView(nextStep);
                    }
                    return true;
                }
            }

            View area = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.VIEW_AREA, "地区*");
            LogManager.getInstance().logInfo("area = " + area);
            if (null != area) {
                ViewOperation.clickView(area);
            }


        } else if (WxUiViewConfig.VIEW_SELECT_CITY.equals(contextName)) {
            View dingwei = v.findViewById(WxUiViewConfig.VIEW_SELECT_CITY_LOCATION);
            LogManager.getInstance().logInfo("dingwei view = " + dingwei);
            if (null != dingwei) {
                if (dingwei.getVisibility() == View.VISIBLE) {
                    if (dingwei instanceof TextView) {
                        TextView dingweiText = (TextView) dingwei;
                        if ("无法获取你的位置信息".equals(dingweiText.getText().toString())) {
                            taskError(v.getContext(), WxTaskManger.TASK_SATE_LOCATION_ERROR, "定位失败");
                            return true;
                        }
                    }
                    LogManager.getInstance().logInfo("定位中");
                    WxUiViewConfig.printViewGroup((ViewGroup) v);
                    return true;
                }
                ViewParent father = dingwei.getParent();
                LogManager.getInstance().logInfo("father ViewParent = " + father);
                if (null != father && father instanceof View) {
                    View fatherView = (View) father;
                    LogManager.getInstance().logInfo("fatherView view = " + fatherView);
                    ViewOperation.clickView(fatherView);
                }
            }
        } else if (WxUiViewConfig.VIEW_EDIT_SIGNATURE.equals(contextName)) {
            String testSign = "开心每一天";

            View inputSign = v.findViewById(WxUiViewConfig.INPUT_SIGN_EDIT);
            if (inputSign instanceof EditText) {
                TextView singTextView = (TextView) inputSign;
                String signContext = singTextView.getText().toString();
                if (!signContext.equals(testSign)) {
                    ViewOperation.setText(singTextView, testSign);
                } else {
                    View saveSign = v.findViewById(WxUiViewConfig.SAVE_SIGN_BUTTON);
                    if (null != saveSign) {
                        ViewOperation.clickView(saveSign);
                    }
                }
            }
        }

        return false;
    }
}
